// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { actionType } from "../common/common_enum";
import GameConfig from "../config/GameConfig";
import EventManager from "../manager/EventManager";
import { Input } from "../utils/InputManager";
import PlayerStatusMachine from "./PlayerStatusMachine";

const {ccclass, property} = cc._decorator;

@ccclass
export default class player extends cc.Component {

    machineOBJ: PlayerStatusMachine = null;
    moveDir = cc.v2(0, 0);

    @property(cc.Node) 
    animHP: cc.Node = null;
    @property(cc.Node)
    battleInfo: cc.Node = null;

    @property(cc.Node)
    skin: cc.Node = null;
    body: cc.RigidBody = null;

    speed = 80;
    jumpDir = cc.v2(0, 0);

    hp_value = 10
    maxhp_value = 10;

    battle_X = 0;
    

    AllAction = []
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
        let actionList = this.battleInfo.getChildByName("actionList")
        EventManager.Inst.on("addAction-player",this, (data)=>{
            console.log("addAction-player",data)
            actionList.getComponent("actionList").addAction(data)
        })
        EventManager.Inst.on("removeAction-player",this, ()=>{
            actionList.getComponent("actionList").removeAction();
        })
    }

    start () {
        this.machineOBJ = new PlayerStatusMachine(this, this.skin);
    }

    getMoveV2() {
        let Sx = 0
        let Sy = 0
        if (Input.axis.x != 0) {
            Sx = Input.axis.x
            //this.skin.scaleX = Sx
            this.setfaceScale(Sx)
        }
        if (Input.axis.y != 0) {
            Sy = Input.axis.y
        }
        if (this.moveDir.x) {
            Sx = this.moveDir.x > 0 ? 1 : -1
            // this.skin.scaleX = Sx
            this.setfaceScale(Sx)
        }

        if (this.moveDir.y) {
            Sy = this.moveDir.y > 0 ? 1 : -1
        }
        return cc.v2(Sx, Sy)
    }

    setfaceScale(Sx) {
        //if (this.machineOBJ.getCurStatusKey() == actionType.Jump) return
        this.skin.scaleX = Sx
    }

    moveLogic(dt){
        if (GameConfig.isBattle) {
            return
        } //战斗中无法移动
        let MoveV2 = this.getMoveV2();
        let dirX = MoveV2.x
        if (MoveV2.x && this.machineOBJ.getCurStatusKey() != actionType.Jump
            && this.machineOBJ.getCurStatusKey() != actionType.Climb
            && this.machineOBJ.getCurStatusKey() != actionType.Attack
        ) {
            //移动状态
            this.machineOBJ.changeStatus(actionType.Walk);

        } else if (!MoveV2.x && this.machineOBJ.getCurStatusKey() != actionType.Jump
            && this.machineOBJ.getCurStatusKey() != actionType.Climb
            && this.machineOBJ.getCurStatusKey() != actionType.Attack
            && this.machineOBJ.getCurStatusKey() != actionType.Stand_to
            && this.machineOBJ.getCurStatusKey() != actionType.Die
        ) {
            //恢复
            this.machineOBJ.changeStatus(actionType.Idle);
        }
        // if (this.machineOBJ.getCurStatusKey() == actionType.Jump) {
        //     dirX = this.jumpDir.x
        // }
        this.node.x += dirX * (this.speed ) * 10 * dt;
    }

    onCollisionEnter(other, self){
        console.log("碰撞",other.node.group)
        if (other.node.group == "Item"){
            //this.inBattle();
            if (!GameConfig.isBattle) {
                EventManager.Inst.emit("inBattle")
                //恢复
                this.machineOBJ.changeStatus(actionType.Idle);
            }
        }
    }

    sethpBar(){
        let value = this.hp_value / this.maxhp_value
        let pro = Math.max(0,value);
        //this.hpBar.progress = pro;
        this.animHP.getComponent("AnimValueProgressHP").setValue(pro);
        // let lab_hp = this.animHP.node.getChildByName("lab_hp");
        // if (lab_hp) {
        //     lab_hp.getComponent(cc.Label).string = (pro*100).toFixed(1)+"%";
        // }
    }

    onHurt(){

    }

    inBattle(target){
        this.battle_X = this.node.x;

    }

    doAction(){
        
    }

    update (dt) {
        this.moveLogic(dt);
    }
}
